package com.yunlong.softpark.controller;

import com.yunlong.softpark.common.enums.BaseErrorCodeEnum;
import com.yunlong.softpark.core.exception.SysException;
import com.yunlong.softpark.core.support.web.controller.BaseController;
import com.yunlong.softpark.core.wrapper.ResultWrapper;
import com.yunlong.softpark.dto.*;
import com.yunlong.softpark.form.ColumnSimpForm;
import com.yunlong.softpark.form.SoftwareSimpForm;
import com.yunlong.softpark.redis.RedisRepository;
import com.yunlong.softpark.service.RecordService;
import com.yunlong.softpark.service.SoftwareService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author: Cui
 * @Date: 2020/7/24
 * @Description:
 */
@Api(value = "SoftwareController", tags = {"软件接口"})
@RestController
@Slf4j
@RequestMapping("/software")
public class SoftwareController extends BaseController<UserInfo> {

    @Autowired
    SoftwareService softwareService;

    @Autowired
    RecordService recordService;

    @Autowired
    RedisRepository redisRepository;

    /**
     * 分类软件主体
     *
     * @return
     */
    @ApiOperation("获取分类页面的板块，分类及栏目")
    @GetMapping("/sort")
    public ResultWrapper getMajorSoft(@RequestParam("page") Integer page, @RequestParam("plateId") Integer plateId) {
        return ResultWrapper.successWithData(softwareService.getMajorSoft(page, plateId));
    }

    /**
     * 首页的软件主体
     *
     * @return
     */
    @ApiOperation("获取首页的栏目")
    @GetMapping("/major")
    public ResultWrapper getFirstMajor() {
        return ResultWrapper.successWithData(softwareService.getFirstMajor());
    }

    /**
     * 模糊搜索
     *
     * @param key
     * @return
     */
    @ApiOperation("模糊搜索")
    @GetMapping("/search")
    public ResultWrapper search(@RequestParam("key") String key) {
        return ResultWrapper.successWithData(softwareService.search(key));
    }

    /**
     * 通过板块获取分类
     *
     * @param plateId
     * @return
     */
    @ApiOperation("通过板块获取分类")
    @GetMapping("/search/{plateId}")
    public ResultWrapper getSortByPlateId(@PathVariable("plateId") Integer plateId) {
        return ResultWrapper.successWithData(softwareService.getSort(plateId));
    }

    /**
     * 通过分类id得到栏目
     *
     * @param sortId
     * @param page
     * @return
     */
    @ApiOperation("通过分类的获取栏目")
    @GetMapping("/searchColumn")
    public ResultWrapper getColumnBySort(@RequestParam("sortId") Integer sortId,
                                         @RequestParam("page") Integer page) {
        return ResultWrapper.successWithData(softwareService.getColumnBySort(sortId, page));
    }

    /**
     * 下载
     *
     * @param softId
     * @return
     */
    @ApiOperation("下载")
    @GetMapping("/download")
    public ResultWrapper downloadSoft(@RequestParam("softId") String softId,
                                      @RequestHeader("ANSWER_ACCESS_TOKEN")String token,
                                      HttpServletRequest request) {
        softwareService.download(softId);
        if(token != null){
            String userId = redisRepository.selectAccessToken(token);
            recordService.saveRecord(request.getRemoteAddr(),2,softId,userId);
        }else {
            recordService.saveRecord(request.getRemoteAddr(),2,softId,null);
        }
        return ResultWrapper.success();
    }

    /**
     * 编辑soft
     *
     * @param softId
     * @return
     */
    @ApiOperation("编辑软件信息，回显")
    @GetMapping("/editSoft")
    public ResultWrapper editSoft(@RequestParam("softId") String softId) {
        EditSoftDto editSoftDto = softwareService.editSoft(softId,getCurrentUserInfo().getUserId());
        return ResultWrapper.successWithData(editSoftDto);
    }

    @ApiOperation("删除软件")
    @GetMapping("/deleteSoft")
    public ResultWrapper deleteSoft(@RequestParam("softId") String softId) {
        softwareService.deleteSoft(softId,getCurrentUserInfo().getUserId());
        return ResultWrapper.success();
    }

    /**
     * 发布软件的获取的三级菜单
     *
     * @return
     */
    @ApiOperation("发布软件页面获取三级分类菜单")
    @GetMapping("/menu")
    public ResultWrapper publishMenu() {
        return ResultWrapper.successWithData(softwareService.publishMenu());
    }

    /**
     * 根据前端传入的columnId获取在栏目栏展示的软件相关信息
     *
     * @param columnSimpForm
     * @return
     */
    @ApiOperation("获取某个栏目的全部版本信息")
    @RequestMapping(path = "/versionShow", method = RequestMethod.POST)
    ResultWrapper getSimpVersionIntroduce(@Valid @RequestBody ColumnSimpForm columnSimpForm, BindingResult bindingResult) {
        try {
            validateParams(bindingResult);
            List<SoftwareSimpVersionDto> list = softwareService.getSimpVersionIntroduce(columnSimpForm.getColumnId());
            return ResultWrapper.successWithData(list);
        } catch (Exception e) {
            log.info("SoftwareController.versionShow");
            return ResultWrapper.failure(e.getMessage());
        }
    }

    /**
     * 根据前端传入的softId获取在软件点开栏上方的简单信息
     *
     * @param softwareSimpForm
     * @return
     */
    @ApiOperation("获取软件的简单介绍")
    @RequestMapping(path = "/simpInto", method = RequestMethod.POST)
    public ResultWrapper getSimpIntroduce(@Valid @RequestBody SoftwareSimpForm softwareSimpForm, BindingResult bindingResult) {
        try {
            validateParams(bindingResult);
            SoftwareSimpIntroDto simpIntroduc =
                    softwareService.getSimpIntroduc(softwareSimpForm.getSoftId());
            return ResultWrapper.successWithData(simpIntroduc);
        } catch (Exception e) {
            log.info("SoftwareController.simpIntro");
            return ResultWrapper.failure(e.getMessage());
        }
    }

    /**
     * 根据前端传入的softid获取软件点开栏的安装步骤右
     *
     * @param softwareSimpForm
     * @return
     */
    @ApiOperation("获取软件的安装步骤")
    @RequestMapping(path = "/detailIntro", method = RequestMethod.POST)
    public ResultWrapper getDetailIntroduce(@RequestBody SoftwareSimpForm softwareSimpForm, BindingResult bindingResult) {
        try {
            validateParams(bindingResult);
            SoftwareDetailDto softwareDetailDto = softwareService.getDetailIntroduce(softwareSimpForm.getSoftId());
            return ResultWrapper.successWithData(softwareDetailDto);
        } catch (Exception e) {
            log.info("SoftwareController.detailIntro");
            return ResultWrapper.failure(e.getMessage());
        }
    }

    /**
     * 根据前端传入的softId返回软件点开栏下方的基本信息
     *
     * @param softwareSimpForm
     * @return
     */
    @ApiOperation("获取软件下方的基本信息")
    @RequestMapping(path = "/basedata", method = RequestMethod.POST)
    public ResultWrapper getBaseData(@Valid @RequestBody SoftwareSimpForm softwareSimpForm, BindingResult bindingResult) {
        try {
            validateParams(bindingResult);
            SoftwareBaseDataDto softwareBaseDataDto = softwareService.getBaseData(softwareSimpForm.getSoftId());
            return ResultWrapper.successWithData(softwareBaseDataDto);
        } catch (Exception e) {
            log.info("SoftwareController.basedata");
            return ResultWrapper.failure(e.getMessage());
        }
    }
}
